package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.UserMapper;
import net.lcheng.model.SysRolePermissionsExample;
import net.lcheng.model.User;
import net.lcheng.model.UserExample;
import net.lcheng.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zwr on 2016/8/27.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User getUserByPhoneOrOpenId(String word) {
        UserExample example = new UserExample();
        example.or().andUserPhoneEqualTo(word);
        example.or().andWxOpenidEqualTo(word);
        List<User> lists = userMapper.selectByExample(example);
        if(lists==null||lists.size()==0) return null;
        else return lists.get(0);
    }

    @Override
    public PageList<User> setUserList(Integer pageIndex, Integer pageSize, String userName, Date start, Date edate) {
        PageHelper.startPage(pageIndex,pageSize);
        User user=new User();
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(userName) == false) {
            criteria.andUserNameLike(userName);
        }
        if (start!=null && edate!=null){
            criteria.andRegTimeBetween(start,edate);
        }
        if(start != null&& edate==null ) {
            criteria.andRegTimeGreaterThanOrEqualTo(start);
        }
        if (edate!=null && start==null){
            criteria.andRegTimeLessThan(start);
        }
        example.or(criteria);
        example.setOrderByClause("user_id desc");
        List<User> list=userMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        PageList pageList = new PageList<User>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage=pageInfo.getPages();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        return pageList;
    }

    @Override
    public int userDelete(String userId) {
        //删除角色
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public User getUserByPhone(String phone) {
        UserExample example = new UserExample();
        example.or().andUserPhoneEqualTo(phone);
//		System.out.println(example.getOredCriteria().toString());
        List<User> list = userMapper.selectByExample(example);
//		System.out.println(list);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            //System.out.println(list.size());
            return list.get(0);
        }
    }

    @Override
    public User getUserByUserId(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
